package com.yinhai.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.lz.util.Utils;
import com.yinhai.activity.R;
import com.yinhai.constant.UrlConstant;
import com.yinhai.model.ExpressionUtil;
import com.yinhai.model.IMList;

public class MSGListSwipeAdapter extends BaseAdapter {
	/**
	 * 上下文对象
	 */
	private List<Map<String, Object>> data;

	private int mRightWidth = 0;
	Context mContext = null;
	private AQuery aq = null;

	public MSGListSwipeAdapter(Context ctx, List<Map<String, Object>> data,
			int rightWidth) {
		this.mContext = ctx;
		this.data = data;
		mRightWidth = rightWidth;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Map<String, Object> getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.msg_sms_list_item, parent, false);
			holder = new ViewHolder();
			holder.item_left = (RelativeLayout) convertView
					.findViewById(R.id.item_left);
			holder.item_right = (RelativeLayout) convertView
					.findViewById(R.id.item_right);

			holder.msg_name = (TextView) convertView
					.findViewById(R.id.msg_name);
			holder.msg_content = (TextView) convertView
					.findViewById(R.id.msg_content);
			holder.item_right_txt = (TextView) convertView
					.findViewById(R.id.item_right_txt);
			holder.number = (TextView) convertView.findViewById(R.id.number);
			holder.msg_date = (TextView) convertView
					.findViewById(R.id.msg_date);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);

		} else {// 有直接获得ViewHolder
			holder = (ViewHolder) convertView.getTag();
		}

		holder.msg_name.setText(data.get(position).get("name") + "");
		holder.msg_content.setText(data.get(position).get("content")+"");
		if (data.get(position).get("number") == null) {
			holder.number.setVisibility(View.GONE);
		} else {

			LinearLayout.LayoutParams lp1 = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			holder.item_left.setLayoutParams(lp1);
			LinearLayout.LayoutParams lp2 = new LayoutParams(mRightWidth,
					LayoutParams.MATCH_PARENT);
			holder.item_right.setLayoutParams(lp2);

			holder.item_right.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mListener != null) {
						mListener.onRightItemClick(v, position);
					}
				}
			});

			int number = Integer.valueOf(data.get(position).get("number")
					.toString());
			if (number <= 0) {
				holder.number.setVisibility(View.GONE);
			} else {
				holder.number.setText(number + "");
				holder.number.setVisibility(View.VISIBLE);
			}

		}

		if (data.get(position).get("date") == null) {
			aq = new AQuery(convertView);
			// holder.msg_date.setText(data.get(position).get("date")+"");
			holder.msg_date.setVisibility(View.GONE);
			IMList imList = (IMList) data.get(position).get("data");
			if (imList!=null&&imList.getIsGroup().equals("0")) { // 个人
				aq.id(R.id.img).image(
						UrlConstant.LOCAL_URI + "getPhoto?phone="
	 							+ imList.getRecId(),
						Utils.getImageOptions(mContext, R.drawable.tel_photo,
								Utils.IsImgSize));
			} else {
				// 群主
				aq.id(R.id.img).image(R.drawable.groupbook_folder);
			}


		} else {
		//	holder.msg_content.setText(data.get(position).get("content") + "");
			holder.msg_date.setText(data.get(position).get("date") + "");
			holder.msg_date.setVisibility(View.VISIBLE);
			// aq.id(R.id.img).image(R.drawable.tel_photo);
		}
		String str = data.get(position).get("content").toString(); // 消息具体内容
		String zhengze = "f0[0-9]{2}|f10[0-7]"; // 正则表达式，用来判断消息内是否有表情
		try {
			SpannableString spannableString = ExpressionUtil
					.getExpressionString(this.mContext, str, zhengze);
			holder.msg_content.setText(spannableString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHolder {
		RelativeLayout item_left;
		RelativeLayout item_right;

		TextView msg_name;
		TextView msg_content;
		TextView number;
		TextView item_right_txt;
		TextView msg_date;
		ImageView img;
	}

	/**
	 * 单击事件监听器
	 */
	private onRightItemClickListener mListener = null;

	public void setOnRightItemClickListener(onRightItemClickListener listener) {
		mListener = listener;
	}

	public interface onRightItemClickListener {
		void onRightItemClick(View v, int position);
	}
}
